package mq

import (
	"fmt"
	"github.com/micro/go-micro/v2/util/log"
	"github.com/streadway/amqp"
	"gomicrodemo/common"
	"time"
)

func Publish(msgs string) {
	mqEntry := common.NewAmqpChannel()
	if err := mqEntry.Channel.Confirm(false); err != nil {
		fmt.Errorf("Channel could not be put into confirm mode: %s", err)
	}
	if err := mqEntry.Channel.Publish(
		common.Exchange,    // publish to an exchange
		common.Topic1, // routing to 0 or more queues
		false,              // mandatory
		false,              // immediate
		amqp.Publishing{
			Headers:         amqp.Table{},
			ContentType:     "text/plain",
			ContentEncoding: "",
			Body:            []byte(msgs),
			DeliveryMode:    amqp.Transient, // 1=non-persistent, 2=persistent
			Priority:        0,              // 0-9
			// a bunch of application/implementation-specific fields
		},
	);err != nil {
		log.Errorf("mq publish fail msg:%v",err)
		return
	}
	log.Infof("mq producer start,time:%v",time.Now().String())
}
